package com.midea.oms;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.midea.oms.Dto.EsCompany;
import com.midea.oms.Dto.JsonResponse;
import com.midea.oms.Dto.PageResponse;
import com.midea.oms.Dto.SiteCodeTemplate;
import com.midea.oms.Dto.Template;
import com.midea.oms.utils.ExcelListener;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: 接口梳理
 * @package: com.midea.oms
 * @className: zerenzhi
 * @author: zhangcan30
 * @description: TODO
 * @date: 2024/3/18 10:26
 * @version: 1.0
 */
public class zerenzhi {
    public static void main(String[] args) throws FileNotFoundException {
        String url = "C:/Users/zhangcan30/Desktop/aaa/aaaa.xlsx";

        Class<SiteCodeTemplate> head = SiteCodeTemplate.class; // 替换为你的数据模型类
        ExcelListener<T> listener = new ExcelListener<>();

        // 直接读取整个 Excel 文件并返回数据列表
        List<SiteCodeTemplate> dataList = EasyExcel.read(url, head, listener).sheet().doReadSync();

        // 处理读取到的数据列表
        dataList.forEach(o -> sendHttpMsg(o));
        System.out.println(dataList);

        String url2 = "C:/Users/zhangcan30/Desktop/aaa/结果.xlsx";
        FileOutputStream outputStream = new FileOutputStream(url2);
        EasyExcel.write(outputStream, SiteCodeTemplate.class).sheet(0, "sheetName").doWrite(dataList);


        List<String> list=new ArrayList<>();

        dataList.forEach(o->{
            String sql="INSERT INTO tms_opms.opms_whitelist\n" +
                "(`type`, site_code, site_name, create_user_code, create_time, update_user_code, update_time, version, delete_flag)\n" +
                "VALUES('NEW_DISPATCH_TOOL_ADOPT_REMARK', '"+o.getSiteCode()+"', '"+o.getSiteName()+"', 'system', '2022-06-21 16:39:15', 'system', '2022-06-21 16:39:15', 0, 0);\n";
            list.add(sql);
        });

        String fileName = "C:/Users/zhangcan30/Desktop/aaa/sql.txt";

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
            for (String line : list) {
                writer.write(line);
                writer.newLine(); // 写入换行符
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

}

    private static void sendHttpMsg(SiteCodeTemplate data) {
        if (ObjectUtils.isEmpty(data) || StringUtils.isEmpty(data.getSiteName())) {
            return;
        }
        try {
            // 创建URL对象
            URL url = new URL("https://anapi.annto.com/api-tms//user/auth/data/site/query");
            String cookie = "MAS_TGC_UAT=eyJhbGciOiJIUzUxMiJ9.WlhsS05tRllRV2xQYVVwRlVsVlphVXhEU21oaVIyTnBUMmxLYTJGWVNXbE1RMHBzWW0xTmFVOXBTa0pOVkVrMFVUQktSRXhWYUZSTmFsVXlTVzR3TGk1WGFYSmFNMWxHY1V3d09EaFpWVE5UYWxveFdHZDNMakJFVmpabFQzRnJOMGxOZFU1Rk4xQkRiR1ZZTmxGd1FrOTFhWHBxUzJkRlIxRlhOblY0T1hoVFVFNHlNbWx6YlRaSVFVaDRjbmd6VFZaRmNsZG9ja1l0VDNSQmQyWlBaMjF4VERGWWFXeFRXbFF0TVhBeWEwZEtOVVp0WmsxcFdtSmpXa0kzWTBkSFJtTlhiRGhTTlZKaGQzRTFPRUpLT1MxQlNrWlJTVGR4Y0hCcVdGUlRTbTAxY0VnNVNtbGtWblJKUWxoQ2NrbEpZVWhKV1RKUE5rSmhkbmxoWmtsM1VtdG5keTVXVEZsMGFIWjFWRVp4TW1zeFRIRjRaWHAxVkU5Ug.WB-_4QlPMJhizzxIu0Y-YjKimDbBOr2zEWtnUKgz2cuniiKkje0YHOJj0SvRpMtzmyFTp3AU7LcNJinIzqWRcw; midea_auth_uat=5F10714B69936818F995850ECC8CC0E44F4AC5CDE7471EFB03B52EFF4B94E396AE37A708B5EC71666463672248D79C0E9120377EE20CB69AFBC84AB4DDA4ED7F78EC43FC3B77976C3B1D9CE5DC3C88073157A437ACD684C20031054C0CCB8CD708004D560801A2A75631F39C39D24383DE62CFF93C144E03A6282EC6D24533F472F61CEED014D8DC543DB62E8CF923FD867EF87ECB73E4058BABF68FDA67D095CA13B96269688BE4964C0B4A4DD8FD6BB76EC9FFB646240C0084935EA60BF98E28661C530D07D695AA960A1B4F27274CFF2751E525A2FBEDB8DE4B32BAB57D04752D06EDCE8CABB04DB3536796FDCDE6428CAC13495473DD0D2E540C48F0C6F7; MAS_TGC=eyJhbGciOiJIUzUxMiJ9.WlhsS05tRllRV2xQYVVwRlVsVlphVXhEU21oaVIyTnBUMmxLYTJGWVNXbE1RMHBzWW0xTmFVOXBTa0pOVkVrMFVUQktSRXhWYUZSTmFsVXlTVzR3TGk1M05rSjJNakZwWTFveFNHb3pUM0JzTjI5V1UzaDNMakozTVZwVFoyRkNTelYyTUV3eVRVRjBVbXQ2WkcxUFJHZGFRVWczTm01clZqQkJlVVJZWTBKUlpURTJTVkJqZDBGWVRUbGpTRmxFVm1GSlUyUTVlamRFZFU1ME0wWXRlakpEYmxGaVlVbHBTM2RyYjFBMlpGUXdTSGs0UzNsaGMxZDZha2xMZGxWVGFsSm5VM1JLZUhaRGFVa3RjRzUzYTA5eVFWYzNabUpXUVU1cFRVUTBPVU5QZGkxb2JHUmZhVGRSTmtrNGRrTnNaRVJoUTBaWVpuaEtSeTE1U0hOWVJWbG1PQzVIYVhsRlpWcEZha2N3Wm5WRVJtSmtTMVY2VTNWUg.pnqdfw65RiKXmLQAFnGRQPoKmMmqkCKZPP4U4lTAH_f034iqbBe_-XEjQmQAIVfi_CdtwgLoMwlym_J6tioq3g; apmUser=zhangcan30; midea_auth=1BA9D52D15239A0D60A10EA12D0C43F21443E9BC43B8CC529CD133600E17B891EB9E840601E77DD99020CB64E05F54A17E05CAA99293D45AD48F069128C674E131003A57033C4688ECF852A191CF6512948EFAFF2AF12CF3F7CD2B01875C78382A093C1FA6134F71B3B9545E338912F450217D874BFC8318B8DE80574EFB21974100C50F7F94A2B697AA7CB282CF674BAE1DE675E54074EE2C0825C3C051BE0193358A77332FC943A9B4C522607B0B1BA92AFD935C7FB345B438AC822CD14CD514FAD74992CB99AB2A29125A2A10A348AE79372CF22263BD413DCC8C023486781AB57D0285F77000C245CA8AE64B6C44D664D77A1116455D44EDD137CD1C0DDC; JSESSIONID=923FCABBB75533051A40D8FF84794239";


            // 创建HttpURLConnection对象
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(true);
            // 设置请求方法为GET
            con.setRequestMethod("POST");

            con.setRequestProperty("Cookie", cookie);
            con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            con.setRequestProperty("Connection", "keep-alive/json");


            OutputStream outputStream = con.getOutputStream();

// 假设你有一个JSON字符串作为请求体
            String jsonInputString = "{\n" +
                "  \"pageSize\": 10,\n" +
                "  \"pageNo\": 1,\n" +
                "  \"escoCompanyNameCn\": \"" + data.getSiteName() + "\"\n" +
                "}";
            byte[] input = jsonInputString.getBytes("utf-8");

            // 打开输出流以发送请求体
            try (OutputStream os = con.getOutputStream()) {
                os.write(input, 0, input.length);
            }

            // 获取响应状态码
            int status = con.getResponseCode();

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            JsonResponse<PageResponse<EsCompany>> jsonResponse = JSON.parseObject(response.toString(), new TypeReference<JsonResponse<PageResponse<EsCompany>>>() {
            });

            try {
                List<EsCompany> list = jsonResponse.getData().getList();
                if (CollectionUtils.isEmpty(list) || list.size() > 1) {
                    return;
                }
                data.setSiteCode(list.get(0).getEscoCompanyNo());

            } catch (Exception e) {
                e.printStackTrace();
            }

            System.out.println("Response status: " + status);
            System.out.println("Response body: " + response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
